
package com.wyzz.hr.bo.impl;

import java.util.List;
import org.apache.axis.utils.StringUtils;
import org.hibernate.criterion.*;

import com.wyzz.hr.bo.IPerfcritsBO;
import com.wyzz.hr.dao.IPerfcritsDAO;
import com.wyzz.hr.pojo.Perfcrits;
import com.wyzz.hr.pojo.Perfcrittype;
import com.wyzz.hr.util.BaseCrit;
import com.wyzz.hr.util.StringUtil;


public class PerfcritsBOImpl implements IPerfcritsBO {

	public PerfcritsBOImpl() {
	}

	public List loadPerfcritsByDC(DetachedCriteria dc) {
		return perfcritsDAO.findByCriteria(dc);
	}

	public Integer getMaxSortId(String pctId) {
		String hql = (new StringBuilder())
				.append(
						"select max(pcSortId) as maxSortId from Perfcrits perf where perf.pcPctId.pctId='")
				.append(pctId).append("'").toString();
		List maxNoList = perfcritsDAO.exeHqlList(hql);
		Integer maxID;
		if (maxNoList == null || maxNoList.size() < 1
				|| maxNoList.get(0) == null)
			maxID = Integer.valueOf(1);
		else
			maxID = Integer.valueOf(1 + ((Integer) (Integer) maxNoList.get(0))
					.intValue());
		return maxID;
	}

	public void saveObject(Perfcrits perfcrits) {
		perfcritsDAO.saveOrupdate(perfcrits);
	}

	public boolean checkObjectByName(String name) {
		DetachedCriteria dc = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Perfcrits.class);
		BaseCrit.addDC(dc, Perfcrits.PROP_PC_NAME, "eq", new String[] { name });
		List object = perfcritsDAO.findByCriteria(dc);
		return object != null && object.size() != 0;
	}

	public void saveObjectByList(List perfcritsList) {
		perfcritsDAO.saveOrupdate(perfcritsList);
	}

	public Perfcrits loadObjectById(String critsId) {
		if (StringUtils.isEmpty(critsId))
			return null;
		DetachedCriteria dc = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Perfcrits.class);
		dc.createAlias(Perfcrits.PROP_PC_PCT_ID, "pcPctId", 1);
		BaseCrit
				.addDC(dc, Perfcrits.PROP_PC_ID, "eq", new String[] { critsId });
		List tempPerfcrits = perfcritsDAO.findByCriteria(dc);
		if (tempPerfcrits.size() >= 1)
			return (Perfcrits) tempPerfcrits.get(0);
		else
			return null;
	}

	public List loadAllCrits() {
		DetachedCriteria dc = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Perfcrits.class);
		dc.addOrder(Order.asc(Perfcrits.PROP_PC_SORT_ID));
		return perfcritsDAO.findByCriteria(dc);
	}

	public List loadAllCritsName() {
		String hql = "select perf.pcName  from Perfcrits perf";
		return perfcritsDAO.exeHqlList(hql);
	}

	public String deleteObjectByIds(String ids[]) {
		String hql = (new StringBuilder())
				.append(
						"select count(*) from Perfplanscore pps where pps.ppsPcId.pcId in(")
				.append(StringUtil.splitForIn(ids)).append(")").toString();
		List tempObject = perfcritsDAO.exeHqlList(hql);
		if (tempObject != null && !"0".equals(tempObject.get(0).toString()))
			return (new StringBuilder())
					.append(
							"\u5458\u5DE5\u8003\u6838\u8BA1\u5212\u6307\u6807\u8BC4\u5206(\u603B\u8868)\u4E2D\u6709")
					.append(tempObject.get(0))
					.append(
							"\u4E2A\u8BB0\u5F55\u5BF9\u8003\u6838\u6307\u6807\u7684\u5F15\u7528\uFF01")
					.toString();
		hql = (new StringBuilder())
				.append(
						"select count(*) from Perfschemacrits psc where psc.pscPcId.pcId in(")
				.append(StringUtil.splitForIn(ids)).append(")").toString();
		tempObject = perfcritsDAO.exeHqlList(hql);
		if (tempObject != null && !"0".equals(tempObject.get(0).toString())) {
			return (new StringBuilder()).append(
					"\u8003\u6838\u65B9\u6848\u6307\u6807\u4E2D\u6709").append(
					tempObject.get(0)).append(
					"\u5BF9\u8003\u6838\u6307\u6807\u7684\u5F15\u7528\uFF01")
					.toString();
		} else {
			hql = (new StringBuilder()).append(
					"delete from Perfcrits perf where perf.pcId in(").append(
					StringUtil.splitForIn(ids)).append(")").toString();
			perfcritsDAO.exeHql(hql);
			return "success";
		}
	}

	public IPerfcritsDAO getPerfcritsDAO() {
		return perfcritsDAO;
	}

	public void setPerfcritsDAO(IPerfcritsDAO perfcritsDAO) {
		this.perfcritsDAO = perfcritsDAO;
	}

	public List loadAllCanUsedPerfcrits() {
		DetachedCriteria dc = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Perfcrits.class);
		dc.add(Restrictions.eq(Perfcrits.PROP_PC_STATUS, Integer.valueOf(1)));
		dc.addOrder(Order.asc(Perfcrits.PROP_PC_SORT_ID));
		return perfcritsDAO.findByCriteria(dc);
	}

	public List getPerfcritsByPerfcrittype(String pctId) {
		DetachedCriteria dc = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Perfcrits.class);
		dc.add(Restrictions.eq(Perfcrits.PROP_PC_STATUS, Integer.valueOf(1)));
		dc.addOrder(Order.asc(Perfcrits.PROP_PC_SORT_ID));
		dc.add(Restrictions.eq(Perfcrits.PROP_PC_PCT_ID,
				new Perfcrittype(pctId)));
		return perfcritsDAO.findByCriteria(dc);
	}

	public void updateSortId(String ids[]) {
		for (int i = 0; i < ids.length; i++)
			perfcritsDAO.exeHql((new StringBuilder()).append(
					"update Perfcrits set pcSortId=").append(i + 1).append(
					" where pcId ='").append(ids[i]).append("'").toString());

	}

	public List searchAllActiveName() {
		String hql = "select pcName from Perfcrits where pcStatus=1";
		return perfcritsDAO.exeHqlList(hql);
	}

	private IPerfcritsDAO perfcritsDAO;
}
